XMLDocument Class
Represents an XML document. It is used for both parsing existing XML data into a DOM document structure or creating an new XML document from scratch.
More information available in parent classes: XMLNode:Object
Constructor
Name | Parameters | Description |
XMLDocument | Creates an empty document. | |
XMLDocument | XML as String | Parses the passed XML string into the document. |
XMLDocument | f as FolderItem | Parses the passed XML file into the document. |
Example
This example creates a very simple XML document.
Dim xml as XmlDocument
Dim root, person, firstname, lastname as XMLNode
xml = New XmlDocument
root = xml.AppendChild(xml.CreateElement("root"))
person = root.AppendChild(xml.CreateElement("person"))
firstname = person.AppendChild(xml.CreateElement("firstname"))
firstname.AppendChild(xml.CreateTextNode("John"))
lastname = person.AppendChild(xml.CreateElement("lastname"))
lastname.AppendChild(xml.CreateTextNode("Doe"))
//create_output is a muti-line EditField
create_output.text = xml.ToString
Dim root, person, firstname, lastname as XMLNode
xml = New XmlDocument
root = xml.AppendChild(xml.CreateElement("root"))
person = root.AppendChild(xml.CreateElement("person"))
firstname = person.AppendChild(xml.CreateElement("firstname"))
firstname.AppendChild(xml.CreateTextNode("John"))
lastname = person.AppendChild(xml.CreateElement("lastname"))
lastname.AppendChild(xml.CreateTextNode("Doe"))
//create_output is a muti-line EditField
create_output.text = xml.ToString
This example adds "ID" and "active" attributes to an element.
Dim xml as XmlDocument
Dim root, node, attNode as XMLNode
xml = New XmlDocument
root = xml.AppendChild(xml.CreateElement("root"))
node = root.AppendChild(xml.CreateElement("person"))
attNode = xml.CreateAttribute("id")
attNode.value = "55"
node.SetAttribute("active", "true")
create_output.text = xml.ToString
Dim root, node, attNode as XMLNode
xml = New XmlDocument
root = xml.AppendChild(xml.CreateElement("root"))
node = root.AppendChild(xml.CreateElement("person"))
attNode = xml.CreateAttribute("id")
attNode.value = "55"
node.SetAttribute("active", "true")
create_output.text = xml.ToString
This example parses XML and displays the number of "Person" nodes and the document text in an EditField.
Dim xdoc as XmlDocument
Dim node as XMLNode
Dim i, count as Integer
Dim out as String
// create a new document
xdoc = New XmlDocument
xdoc.PreserveWhitespace = False
// load and parse the xml in the EditField, parse_InputText
xdoc.LoadXml(parse_inputText.Text)
// lets first do some simple, static traversing of the xml
// this doesn't do any error checking to make sure stuff exists
// so if the document changes, it will break.
count = xdoc.DocumentElement.childCount
out = "People: " + Str(count) //number of Person nodes
out = out + EndOfLine
// childNodes are 0 based.
For i = 0 to count - 1
node = xdoc.DocumentElement.Child(i)
// this gets the textnode of the <firstname> node
out = out + node.FirstChild.FirstChild.Value
// this gets the textnode of the <lastname> node
out = out + " " + node.LastChild.FirstChild.Value
out = out + EndOfLine
next
//display results in the EditField parse_output
parse_output.text = out
Dim node as XMLNode
Dim i, count as Integer
Dim out as String
// create a new document
xdoc = New XmlDocument
xdoc.PreserveWhitespace = False
// load and parse the xml in the EditField, parse_InputText
xdoc.LoadXml(parse_inputText.Text)
// lets first do some simple, static traversing of the xml
// this doesn't do any error checking to make sure stuff exists
// so if the document changes, it will break.
count = xdoc.DocumentElement.childCount
out = "People: " + Str(count) //number of Person nodes
out = out + EndOfLine
// childNodes are 0 based.
For i = 0 to count - 1
node = xdoc.DocumentElement.Child(i)
// this gets the textnode of the <firstname> node
out = out + node.FirstChild.FirstChild.Value
// this gets the textnode of the <lastname> node
out = out + " " + node.LastChild.FirstChild.Value
out = out + EndOfLine
next
//display results in the EditField parse_output
parse_output.text = out
See Also
XMLAttribute, XMLComment, XMLCDATASection, XMLDOMException, XMLElement, XMLException, XMLNode, XMLNodeList, XMLProcessingInstruction, XMLTextNode classes.